<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件名称" prop="eventName">
        <el-input v-model="queryParams.eventName" placeholder="请输入事件名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['manager:exigencyEvent:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['manager:exigencyEvent:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['manager:exigencyEvent:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['manager:exigencyEvent:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exigencyEventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="事件名称" align="center" prop="eventName" />
      <el-table-column label="事件编码" align="center" prop="code" />
      <el-table-column label="分类" align="center">
        <template slot-scope="scope">
          {{ scope.row.cate == 0 ? '事件服务器类型' : '手工录入类型' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['manager:exigencyEvent:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['manager:exigencyEvent:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改事件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事件编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入事件编码" />
        </el-form-item>
        <el-form-item label="分类" prop="cate">
          <el-select v-model="form.cate" style="width: 100%" placeholder="请选择分类">
            <el-option label="事件服务器类型" value="0"></el-option>
            <el-option label="手工录入类型" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联标签" prop="planName">
          <el-select v-model="form.planDefaultTagList" placeholder="请选择" style="width:100%;" multiple>
            <el-option v-for="item in eventuserList" :key="item.id" :label="item.tagName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事件名称" prop="eventName">
          <el-input v-model="form.eventName" placeholder="请输入事件名称" />
        </el-form-item>

        <el-form-item>
          <template v-for="(item, index) in form.sceneList">
            <div :key="index" style="display: flex; margin-bottom:10px;">
              <el-form label-width="80px" style="flex:1;">
                <el-form-item :label="'情景内容' + (index + 1)">
                  <el-input v-model="item.sceneContent" placeholder="请输入情景内容" />
                </el-form-item>
        
              </el-form>
              <el-button type="primary" style="height: 34px; margin:0 10px;" v-if="index == 0" @click="increaseadd()">增加</el-button>
              <el-button type="primary" style="height: 34px; margin:0 10px;" v-if="index > 0" @click="deleteadd(index)">删除</el-button>
            </div>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExigencyEvent, getExigencyEvent, delExigencyEvent, addExigencyEvent, updateExigencyEvent, exportExigencyEvent, putExigencyEvent, deleteEventsceneId, deleteEventdisposalConceptId } from '@/api/event/eventType';

import { gettagList } from '@/api/event/eventPlan';
export default {
  name: 'ExigencyEvent',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事件表格数据
      exigencyEventList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eventName: null
      },
      eventuserList: [], // 标签
      // 表单参数
      form: {
        code: '', //事件编码
        eventName: '', //事件名称
        sceneList: [
          {
            sceneContent: '' //情景内容
            //disposalConceptList: [
            //  {
            //    disposalContent: '' //方案内容
            //  }
            //]
          }
        ]
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.eventlistadd();
  },
  methods: {
    /** 查询事件列表 */
    getList() {
      this.loading = true;
      listExigencyEvent(this.queryParams).then(response => {
        this.exigencyEventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        code: '', //事件编码
        eventName: '', //事件名称
        planDefaultTagList: [],
        sceneList: [
          {
            sceneContent: '' //情景内容
            //disposalConceptList: [
            //  {
            //    disposalContent: '' //方案内容
            //  }
            //]
          }
        ]
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加事件';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      putExigencyEvent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改事件';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExigencyEvent(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addExigencyEvent(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除事件编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delExigencyEvent(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有事件数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportExigencyEvent(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    // 增加
    increaseadd() {
      // this.form = {
      //   code: '', //事件编码
      //   eventName: '', //事件名称
      //   sceneList: [
      //     {
      //       sceneContent: '1', //情景内容
      //       disposalConceptList: [
      //         {
      //           disposalContent: '' //方案内容
      //         }
      //       ]
      //     }
      //   ]
      // };

      this.form.sceneList.push({
        sceneContent: '' //情景内容
        //disposalConceptList: [
        //  {
        //    disposalContent: '' //方案内容
        //  }
        //]
      });
    },
    // 删除
    deleteadd(index) {
      if (this.form.sceneList[index].id) {
        deleteEventsceneId(this.form.sceneList[index].id).then(res => {
          console.log(res);
        });
      }
      this.form.sceneList.splice(index, 1);
    },
    // 新增
    //increaseaddsub(index, indexs) {
    //  this.form.sceneList[index].disposalConceptList.push({
    //    disposalContent: '' //方案内容
    //  });
    //},
    //// 删除
    //deleteaddsub(index, indexs) {
    //  if (this.form.sceneList[index].disposalConceptList[indexs].id) {
    //    deleteEventdisposalConceptId(this.form.sceneList[index].disposalConceptList[indexs].id).then(res => {
    //      console.log(res);
    //    });
    //  }
    //
    //  this.form.sceneList[index].disposalConceptList.splice(indexs, 1);
    //}
		eventlistadd() {
      gettagList().then(res => {
        console.log(res);
        this.eventuserList = res.data;
      });
    }
  }
};
</script>
